package com.blue.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.entity.Relation;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.po.system.FileInfoPO;
import com.blue.service.system.IFileInfoService;
import com.blue.vo.system.FileInfoVO;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统文件信息表 前端控制器
 * </p>
 *
 * @author nanjie.chen
 * @since 2023-09-17
 */
@RestController
@RequestMapping("/system/fileInfo")
@Tag(name = "系统文件管理")
public class FileInfoController {
    @Autowired
    private IFileInfoService fileInfoService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("fileName,type")
    public ResponseResult<?> pageList(FileInfoVO requestVO,
                                      @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        String startTime = requestVO.getStartTime();
        String endTime = requestVO.getEndTime();
        FileInfoPO fileInfoPO = ConvertUtils.convert(FileInfoPO.class, requestVO);
        Relation relation = new Relation("username", "author_user t1 on t.creator_id = t1.user_id");
        MPJQueryWrapper<FileInfoPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(fileInfoPO,relation);
        queryWrapper.ge(StringUtils.isNotBlank(startTime), "t.create_time", startTime);
        queryWrapper.le(StringUtils.isNotBlank(endTime), "t.create_time", endTime);
        Page<FileInfoPO> page = new Page<>(pageNum, pageSize);
        Page<FileInfoPO> pageResult = fileInfoService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除文件")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        fileInfoService.deleteFile(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/synchronous")
    @Operation(summary = "同步更新")
    public ResponseResult<?> synchronous() {
        fileInfoService.synchronous();
        return ResponseResult.okResult();
    }
}
